package com.jackson0714.passjava.gateway.config;

import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import reactor.core.publisher.Mono;

/**
  * @Description:网关限流配置
  * @Version v1.0.0
  * @author zhangpeng148
  * @date 2022/1/25 15:44
  */
@Configuration
public class RateLimiterConfig {

    @Primary
    @Bean(value = "remoteAddrKeyResolver")
    public KeyResolver remoteAddrKeyResolver() {
        return exchange -> Mono.just(exchange.getRequest().getRemoteAddress().getAddress().getHostAddress());
    }


    @Bean(value = "requestApiResolver")
    public KeyResolver requestApiResolver() {
        return exchange -> Mono.just(exchange.getRequest().getPath().value());
    }
}
